package dangs.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import dangs.service.QnAService;
import dangs.vo.ListVO;
import dangs.vo.MemberVO;
import dangs.vo.ProductVO;
import dangs.vo.QnAVO;

public class QnAController extends MultiActionController{
	private QnAVO qvo;
	private ProductVO pvo;
	private QnAService service;
	public void setQnAService(QnAService service){
		this.service = service;
	}
	
	Boolean isViewCntPlus=false;
	
	public ModelAndView insertContent(HttpServletRequest request, HttpServletResponse response, QnAVO qvo) throws Exception {
		HttpSession session = request.getSession();
		String id = (String) session.getAttribute("id");
		MemberVO mvo = new MemberVO(id);
		String productCode = request.getParameter("productCode");
		qvo.setProductCode(productCode);
		qvo.setMvo(mvo);
		System.out.println(qvo);
		service.insertContent(qvo);
		return new ModelAndView("write_success", "qvo", qvo);
	}
	public ModelAndView selectContent(HttpServletRequest request, HttpServletResponse response, QnAVO qvo) throws Exception {
		isViewCntPlus=true;
		int page=1;
		if(request.getParameter("page")==null) {
			page=1;
		}
		else {
			page=Integer.parseInt(request.getParameter("page"));
		}
		int qnaNo=Integer.parseInt(request.getParameter("qnaNo"));
		qvo.setQnaNo(qnaNo);
		qvo=service.selectContent(qvo, isViewCntPlus);
		request.setAttribute("page", page);
		return new ModelAndView("show_content", "qvo", qvo);
	}
	public ModelAndView updateContentForm(HttpServletRequest request, HttpServletResponse response, QnAVO qvo) throws Exception {
		isViewCntPlus=false;
		int page=Integer.parseInt(request.getParameter("page"));
		int qnaNo = Integer.parseInt(request.getParameter("qnaNo"));
		qvo.setQnaNo(qnaNo);
		qvo = service.selectContent(qvo, isViewCntPlus);
		request.setAttribute("page", page);
		String productCode = request.getParameter("productCode");
		request.setAttribute("productCode", productCode);
		return new ModelAndView("modify_qna", "qvo", qvo);
	}
	public ModelAndView updateContent(HttpServletRequest request, HttpServletResponse response, QnAVO qvo) throws Exception {
		System.out.println(qvo);
		HttpSession session = request.getSession();
		String id = (String) session.getAttribute("id");
		MemberVO mvo = new MemberVO(id);
		String productCode = request.getParameter("productCode");
		int qnaNo = Integer.parseInt(request.getParameter("qnaNo"));
		qvo.setQnaNo(qnaNo);
		qvo.setProductCode(productCode);
		qvo.setMvo(mvo);
		service.updateContent(qvo);
		int page=Integer.parseInt(request.getParameter("page"));
		request.setAttribute("page", page);
		return new ModelAndView("write_success", "qvo", qvo);
	}
	public ModelAndView deleteContent(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int page=Integer.parseInt(request.getParameter("page"));
		int qnaNo=Integer.parseInt(request.getParameter("qnaNo"));
		QnAVO qvo=new QnAVO();
		qvo.setQnaNo(qnaNo);
		service.deleteContent(qvo, page);
		request.setAttribute("page", page);
		return new ModelAndView("delete_success");
	}
	public ModelAndView selectContentList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int page=1;
		if(request.getParameter("page") != null){
			page = Integer.parseInt(request.getParameter("page"));
		}
		System.out.println(page);
		String productCode = request.getParameter("productCode");
		ListVO lvo=service.getList(productCode, page);
		request.setAttribute("page", page);
		if(lvo.getList().isEmpty()){
			return new ModelAndView("list_qna", "lvo", null);
		}else{
			return new ModelAndView("list_qna", "lvo", lvo);
		}
	}
	public ModelAndView replyForm(HttpServletRequest request, HttpServletResponse response, QnAVO qvo) throws Exception {
		int qnaNo = Integer.parseInt(request.getParameter("qnaNo"));
		QnAVO re=new QnAVO();
		re.setQnaNo(qnaNo);
		qvo = service.selectContent(re, isViewCntPlus);
		return new ModelAndView("reply_qna", "qvo", qvo);
	}
	public ModelAndView replyContent(HttpServletRequest request, HttpServletResponse response, QnAVO qvo) throws Exception {
		HttpSession session = request.getSession();
		String id = (String) session.getAttribute("id");
		MemberVO mvo = new MemberVO(id);
		String productCode = request.getParameter("productCode");
		qvo.setProductCode(productCode);
		qvo.setMvo(mvo);
		System.out.println(qvo);
		service.replyContent(qvo);
		return new ModelAndView("write_success", "qvo", qvo);
	}
	public ModelAndView registerForm(HttpServletRequest request, HttpServletResponse response, QnAVO qvo) throws Exception {
		return new ModelAndView("register_qna");
	}
}